package com.xy.p2p.action.admin;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.p2p.model.Tmemberbadcredit;
import com.xy.p2p.service.TmemberbadcreditService;



@Controller
public class TmemberbadcreditAction{
	private static final Logger log = LoggerFactory.getLogger(TmemberbadcreditAction.class);
	public TmemberbadcreditAction() {
	}
 
 
 	/**
	 * 会员不良信用记录表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbadcreditAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tmemberbadcredit", new Tmemberbadcredit());
        setDict(model);
		return "tmemberbadcreditEdit";
	}

	/**
	 * 会员不良信用记录表新增保存
	 * 
	 * @param tmemberbadcredit
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tmemberbadcreditSave.do")
	public String save(Tmemberbadcredit tmemberbadcredit, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tmemberbadcredit, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tmemberbadcredit = tmemberbadcreditService.save(tmemberbadcredit);
		return "redirect:v_tmemberbadcreditList.do";
	}

	/**
	 * 会员不良信用记录表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tmemberbadcreditDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tmemberbadcreditService.remove(id);
			}
		}
		return "redirect:v_tmemberbadcreditList.do";
	}

	/**
	 * 会员不良信用记录表编辑页面
	 * 
	 * @param mbcid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbadcreditEdit.do")
	public String edit(long mbcid, HttpServletRequest request, ModelMap model) {
		Tmemberbadcredit tmemberbadcredit = tmemberbadcreditService.get(mbcid);
		model.addAttribute("tmemberbadcredit", tmemberbadcredit);
        setDict(model);
		return "tmemberbadcreditEdit";
	}

	/**
	 * 会员不良信用记录表详情
	 * 
	 * @param mbcid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbadcreditDetail.do")
	public String detail(long mbcid, HttpServletRequest request, ModelMap model) {
		Tmemberbadcredit tmemberbadcredit = tmemberbadcreditService.get(mbcid);
		model.addAttribute("tmemberbadcredit", tmemberbadcredit);
        setDict(model);
		return "tmemberbadcreditView";
	}

	/**
	 * 会员不良信用记录表查询（分页）
	 * 
	 * @param tmemberbadcredit
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbadcreditList.do")
	public String listPage(Tmemberbadcredit tmemberbadcredit, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tmemberbadcreditService.getTmemberbadcreditByPage(toMap(tmemberbadcredit),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tmemberbadcreditList";
	}

	/**
	 * 会员不良信用记录表查询
	 * 
	 * @param tmemberbadcredit
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tmemberbadcreditList1.do")
	public String list(Tmemberbadcredit tmemberbadcredit, HttpServletRequest request, ModelMap model) {
		List<Tmemberbadcredit> list = tmemberbadcreditService.getTmemberbadcredit(toMap(tmemberbadcredit));
		model.addAttribute("list", list);
        setDict(model);
		return "tmemberbadcreditList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tmemberbadcredit
	 * @return
	 */	private Map<String,Object> toMap(Tmemberbadcredit tmemberbadcredit){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tmemberbadcredit.getMbcid()!=null&&!tmemberbadcredit.getMbcid().equals("")){
			map.put("mbcid", tmemberbadcredit.getMbcid());
		}
		if(tmemberbadcredit.getTmember()!=null&&!tmemberbadcredit.getTmember().equals("")){
			map.put("Tmember", tmemberbadcredit.getTmember());
		}
		if(tmemberbadcredit.getBadcreditdesc()!=null&&!tmemberbadcredit.getBadcreditdesc().equals("")){
			map.put("badcreditdesc", tmemberbadcredit.getBadcreditdesc());
		}
		if(tmemberbadcredit.getBadcreditlevel()!=null&&!tmemberbadcredit.getBadcreditlevel().equals("")){
			map.put("badcreditlevel", tmemberbadcredit.getBadcreditlevel());
		}
		if(tmemberbadcredit.getStatus()!=null&&!tmemberbadcredit.getStatus().equals("")){
			map.put("status", tmemberbadcredit.getStatus());
		}
		if(tmemberbadcredit.getRemark()!=null&&!tmemberbadcredit.getRemark().equals("")){
			map.put("remark", tmemberbadcredit.getRemark());
		}
		if(tmemberbadcredit.getBccddesc()!=null&&!tmemberbadcredit.getBccddesc().equals("")){
			map.put("bccddesc", tmemberbadcredit.getBccddesc());
		}
		if(tmemberbadcredit.getBccdatetime()!=null&&!tmemberbadcredit.getBccdatetime().equals("")){
			map.put("bccdatetime", tmemberbadcredit.getBccdatetime());
		}
		if(tmemberbadcredit.getBadcreditdatetime()!=null&&!tmemberbadcredit.getBadcreditdatetime().equals("")){
			map.put("badcreditdatetime", tmemberbadcredit.getBadcreditdatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("badcreditlevelMap", DictUtils.getDictByType(DictUtils.TMEMBERBADCREDIT_BADCREDITLEVEL));
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TMEMBERBADCREDIT_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tmemberbadcredit tmemberbadcredit, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TmemberbadcreditService tmemberbadcreditService;   
}